/**
 * ZdConfirm 确认弹窗组件入口
 *
 * @description 自定义element MessageBox组件
 * @author liyujie
 * @date 2021/06/17
 * @param title - 标题
 * @param message - 提示信息
 * @param type - 类型 warning success error info
 * @param confirmButtonText - 确认按钮文本
 * @param cancelButtonText - 取消按钮文本
 */

import Vue from 'vue';
import ConfirmBox from './index.vue';
const ConfirmBoxConstructor = Vue.extend(ConfirmBox);

let instance;
const initInstance = (title, message, type = 'warning', confirmButtonText = '确认', cancelButtonText = '取消', hasCancelButton = true, showClose = false) => {
    instance = new ConfirmBoxConstructor({
        el: document.createElement('div'),
        data() {
            return {
                title,
                message,
                type,
                confirmButtonText,
                cancelButtonText,
                hasCancelButton,
                showClose
            };
        },
    });
};


const showConfirm = (message, title, options = {}) => {
    let { type, confirmButtonText, cancelButtonText, hasCancelButton, showClose } = options;

    return new Promise((resolve, reject) => {
        initInstance(title, message, type, confirmButtonText, cancelButtonText, hasCancelButton, showClose);
        instance.callback = action => {
            if (action === 'confirm') {
                resolve();
            } else if (action === 'cancel') {
                reject();
            }
        };
        document.body.appendChild(instance.$el);
        Vue.nextTick(() => {
            instance.visible = true;
        });
    });
};


function registryConfirm() {
    Vue.prototype.$zdConfirm = showConfirm;
}
export default registryConfirm;
